// pages/yyc/index/index.js
const util = require("../../utils/util.js");
const config = require("../../utils/config.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputShowed: false, //搜索
    inputVal: "",
    userId: "",
    searchBookList:[],//查询结果
    ImageUrl: config.ImageUrl,
    DownloadFileUrl: config.DownloadFileUrl,
    bookList: [], ///目录列表
    bookDetailList: [], //每个目录下的书籍
    activeIndex: 0, //当前所在的滑块
    index: 0 //滑块索引
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    util.setBarColor()
    var currentCategory = {
      "id": 1
    }
    let userId = wx.getStorageSync("userInfo").id;
    this.setData({
      userId: userId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    var that = this
    that.getData();
    that.getCatalogList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  getData: function() {
    var that = this;
    let url = config.BaseUrl + "t/book/list";
    util.request(url, "GET").then(function(res) {
      if (res.code == 200) {
        that.setData({
          bookList: res.data.list
        })
      }
    })
  },

  getCatalogList: function(id) {
    var that = this;
    let bookId = parseInt(id) + 1;
    let url = config.BaseUrl + "t/catalog/queryAll";
    util.request(url, "GET").then(function(res) {
      if (res.code == 200) {
        that.setData({
          bookDetailList: res.data[1],
          catalogList: res.data
        })
      }
    })
  },


  //选择tabs
  tabClick: function(e) {
    var that = this
    if (e.currentTarget.id != "") {
      that.setData({
        activeIndex: e.currentTarget.id
      });
    } else {
      that.setData({
        activeIndex: e.detail.current
      })
    }
    let catalogList = that.data.catalogList;
    let index = parseInt(that.data.activeIndex) + 1;
    if (catalogList[index] == undefined) {
      let data = []
      that.setData({
        bookDetailList: data
      })
    } else {
      that.setData({
        bookDetailList: catalogList[index]
      })
    }
  },

  //下载并预览
  toCatalogDetail: function(e) {
    var that = this
    wx.downloadFile({
      url: that.data.DownloadFileUrl + e.currentTarget.dataset.url,
      header: {},
      success: function(res) {
        var filePath = res.tempFilePath;
        console.log(filePath);
        wx.openDocument({
          filePath: filePath,
          success: function(res) {
            console.log('打开文档成功')
            //将浏览记录保持到数据库
            let data = {
              "userId" : that.data.userId,
              "bookId" : e.currentTarget.dataset.id
            }
            let url = config.BaseUrl + "t/user/book/add";
            util.request(url, data, "POST")
          },
          fail: function(res) {
            console.log(res);
          },
          complete: function(res) {
            console.log(res);
          }
        })
      },
      fail: function(res) {
        console.log('文件下载失败');
      },
      complete: function(res) {},
    })
  },

  //搜索功能相关
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  bindConfirm:function(){
    var that = this;
    let catalogName = that.data.inputVal;
    let url = config.BaseUrl + "t/catalog/queryCatalogByBookName?catalogName="+catalogName;
    util.request(url, "GET").then(function (res) {
      if (res.code == 200) {
        that.setData({
          searchBookList: res.data,
        })
      }
    })
  },
  itemPress:function(e){
    var that = this;
    wx.showModal({
      title: '添加图书',
      content: '确定将当前图书添加到我的阅读？',
      confirmText: "确定",
      cancelText: "取消",
      success: function (res) {
        console.log(res);
        if (res.confirm) {
          let url = config.BaseUrl + "t/user/book/add";
          var bookId = e.currentTarget.dataset.id;
          var userId = that.data.userId;
          let tUserBook = {
            "bookId": bookId,
            "userId": userId
          }
          util.request(url, tUserBook, "POST").then(function (res) {
            if (res.code == 200) {
              wx.showToast({
                title: '添加成功'
              })
            }
          })
        } else {
          console.log('用户点击辅助操作')
        }
      }
    });
  }
})